from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui

import sys

class QFileDialogTest(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(QFileDialogTest, self).__init__(parent)

        self.setWindowTitle("QFileDialogTest例子")

        self.btn_load_image = QtWidgets.QPushButton("加载图片")
        self.btn_load_image.clicked.connect(self.btn_load_image_clicked)

        self.label_show_image = QtWidgets.QLabel("")

        self.btn_load_text = QtWidgets.QPushButton("加载文本")
        self.btn_load_text.clicked.connect(self.btn_load_text_clicked)

        self.text_edit_show_text = QtWidgets.QTextEdit()

        _layout = QtWidgets.QVBoxLayout()
        _layout.addWidget(self.btn_load_image)
        _layout.addWidget(self.label_show_image)
        _layout.addWidget(self.btn_load_text)
        _layout.addWidget(self.text_edit_show_text)

        self.setLayout(_layout)

    def btn_load_image_clicked(self):
        fname, _ = QtWidgets.QFileDialog.getOpenFileName(self, "open file", "f:\\", "ImageFile(*.jpg, *.png)")
        self.label_show_image.setPixmap(QtGui.QPixmap(fname))

    def btn_load_text_clicked(self):
        dlg = QtWidgets.QFileDialog()
        dlg.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
        dlg.setFilter(QtCore.QDir.Files)

        if dlg.exec_():
            filenames = dlg.selectedFiles()
            with open(filenames[0], 'r', encoding='utf-8') as f:
                self.text_edit_show_text.setText(f.read())

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    QtWidgets.QApplication.setStyle("Fusion")
    win = QFileDialogTest()
    win.show()
    sys.exit(app.exec_())